//mcp2515.c
#include "pin_mux.h"
#include "clock_config.h"
#include "fsl_debug_console.h"
#include "fsl_gpio.h"
#include "mcp2515.h"
#include "led.h"



uint8_t id[4]={0x05,0x08,0x05,0x82};	 					//发送的ID地址
uint8_t rxdat[10];

gpio_pin_config_t mcp2515_output_config = {kGPIO_DigitalOutput, 0, kGPIO_NoIntmode};
gpio_pin_config_t mcp2515_input_config = {kGPIO_DigitalInput, 0, kGPIO_NoIntmode};

gpio_pin_config_t can_int_config = {kGPIO_DigitalInput, 0, kGPIO_IntFallingEdge, };

//端口初始化
void mcp2515_port_init(void)
{
	GPIO_PinInit(E53_SPI_SCK, E53_SPI_SCK_PIN, &mcp2515_output_config);   	//CAN_SCK
	GPIO_PinInit(E53_SPI_MOSI, E53_SPI_MOSI_PIN, &mcp2515_output_config);	//CAN_MOSI
	GPIO_PinInit(E53_SPI_MISO, E53_SPI_MISO_PIN, &mcp2515_input_config);	//CAN_MISO
	GPIO_PinInit(CAN_CS, CAN_CS_PIN, &mcp2515_output_config);				//CAN_CS
	//中断设置
	EnableIRQ(EXAMPLE_CAN_IRQ);
	GPIO_PinInit(EXAMPLE_CAN_GPIO, EXAMPLE_CAN_GPIO_PIN, &can_int_config);
	//使能GPIO中断
	GPIO_PortEnableInterrupts(EXAMPLE_CAN_GPIO, 1U << EXAMPLE_CAN_GPIO_PIN);


}

void EXAMPLE_CAN_IRQHandler(void)
{
    GPIO_PortClearInterruptFlags(EXAMPLE_CAN_GPIO, 1U << EXAMPLE_CAN_GPIO_PIN);

    CAN_Receive(rxdat);
    CAN_Eint_Clear();
    E53_GPIO3_TOG();

    SDK_ISR_EXIT_BARRIER;
}


void dels(uint32_t tt)
{
	uint32_t ds=0;
	for(;ds<tt;ds++);
}

uint8_t SPI_Read(void)
{
	uint8_t n=8;
	uint8_t tdata;

	E53_SPI_SCK_LOW();
	while(n--)
	{
		dels(100);
		E53_SPI_SCK_HIGH();
		tdata=tdata<<1;
		if( 1 == GPIO_PinRead(E53_SPI_MISO, E53_SPI_MISO_PIN))
			tdata=tdata|0x01;
		else
			tdata=tdata&0xfe;
		E53_SPI_SCK_LOW();
	}
	return tdata;
}


void SPI_Write(uint8_t ch)
{
	uint8_t n=8;

	E53_SPI_SCK_LOW();
	while(n--)
	{
		E53_SPI_SCK_LOW();
		dels(100);
		if((ch&0x80)==0x80)
		{
			E53_SPI_MOSI_HIGH();
		}
		else
		{
			E53_SPI_MOSI_LOW();
		}
		dels(100);
		E53_SPI_SCK_HIGH();
		dels(100);
		ch=ch<<1;
	}
}

void SPI_ByteWrite(uint8_t addr, uint8_t value)
{
	CAN_CS_LOW();
	SPI_Write(CAN_WRITE);
	SPI_Write(addr);
	SPI_Write(value);
	CAN_CS_HIGH();
}

uint8_t SPI_ByteRead(uint8_t addr)
{
	uint8_t tempdata;

	CAN_CS_LOW();
	SPI_Write(CAN_READ);
	SPI_Write(addr);
	tempdata=SPI_Read();
	CAN_CS_HIGH();
	return tempdata;
}


void SPI_Reset(void)
{
	CAN_CS_LOW();
	SPI_Write(CAN_RESET);
	CAN_CS_HIGH();
}


void SPI_BitModityMcp2515(uint8_t addr, uint8_t mask, uint8_t udata)
{

     CAN_CS_LOW();
     SPI_Write(CAN_BIT_MODIFY);
     SPI_Write(addr);
     SPI_Write(mask);
     SPI_Write(udata);
     CAN_CS_HIGH();
     return;
}

//写连续N个字节数据
void SPI_WriteRegister(uint8_t addr, uint8_t *dat, uint8_t length)
{
	uint8_t i;

	CAN_CS_LOW();
	SPI_Write(CAN_WRITE);
	SPI_Write(addr);
	for (i=0; i<length ;i++ )
	{
		SPI_Write(*dat++);
	}
	CAN_CS_HIGH();
}

//MCP2515初始化
void Init_MCP2515(void)
{
	uint8_t temp[4] = { 0, 0, 0, 0 };

	mcp2515_port_init();

	SPI_Reset();
	dels(1000);
	SPI_BitModityMcp2515(CANCTRL,0xE0,0x80);

	SPI_ByteWrite(CNF1,CAN_100Kbps);

	SPI_ByteWrite(CNF2,0x80|PHSEG1_3TQ|PRSEG_1TQ);

	SPI_ByteWrite(CNF3,PHSEG2_3TQ);

 	SPI_ByteWrite(CANINTF,0x00);
 	SPI_ByteWrite(CANINTE,0x01);

	SPI_ByteWrite(RXB0CTRL,0x60);
	SPI_ByteWrite(RXB1CTRL,0x60);

	SPI_ByteWrite(RXF0SIDH,0x01);
	SPI_ByteWrite(RXF0SIDL,0x08);
	SPI_ByteWrite(RXF0EID8,0x05);
	SPI_ByteWrite(RXF0EID0,0x11);

	SPI_ByteWrite(RXM0SIDH,0xff);
	SPI_ByteWrite(RXM0SIDL,0xff);
	SPI_ByteWrite(RXM0EID8,0xff);
	SPI_ByteWrite(RXM0EID0,0xff);

//	SPI_WriteRegister( RXF0SIDH, temp, 4 );
	SPI_WriteRegister( RXF1SIDH, temp, 4 );
	SPI_WriteRegister( RXF2SIDH, temp, 4 );
	SPI_WriteRegister( RXF3SIDH, temp, 4 );
	SPI_WriteRegister( RXF4SIDH, temp, 4 );
	SPI_WriteRegister( RXF5SIDH, temp, 4 );
//	SPI_WriteRegister( RXM0SIDH, temp, 4 );
	SPI_WriteRegister( RXM1SIDH, temp, 4 );

	SPI_ByteWrite( BFPCTRL, 0 );
	SPI_ByteWrite( TXRTSCTRL, 0 );
	SPI_BitModityMcp2515(CANCTRL,0xE0,0x00);

}


void CAN_Send(uint8_t *CAN_TX_Buf)
{
	SPI_BitModityMcp2515(TXB0CTRL,0x08,0x00);
	SPI_ByteWrite(TXB0SIDH,id[0]);
	SPI_ByteWrite(TXB0SIDL,id[1]);
	SPI_ByteWrite(TXB0EID8,id[2]);
	SPI_ByteWrite(TXB0EID0,id[3]);

	SPI_ByteWrite(TXB0DLC,DLC_8);
	SPI_ByteWrite(TXB0D0,CAN_TX_Buf[0]);
	SPI_ByteWrite(TXB0D1,CAN_TX_Buf[1]);
	SPI_ByteWrite(TXB0D2,CAN_TX_Buf[2]);
	SPI_ByteWrite(TXB0D3,CAN_TX_Buf[3]);
	SPI_ByteWrite(TXB0D4,CAN_TX_Buf[4]);
	SPI_ByteWrite(TXB0D5,CAN_TX_Buf[5]);
	SPI_ByteWrite(TXB0D6,CAN_TX_Buf[6]);
	SPI_ByteWrite(TXB0D7,CAN_TX_Buf[7]);
	CAN_CS_LOW();
	SPI_Write(CAN_RTS | 0x01);
	CAN_CS_HIGH();

}

void CAN_Receive(uint8_t *CAN_RX_Buf)
{
	CAN_RX_Buf[0]=SPI_ByteRead(RXB0D0);
	CAN_RX_Buf[1]=SPI_ByteRead(RXB0D1);
	CAN_RX_Buf[2]=SPI_ByteRead(RXB0D2);
	CAN_RX_Buf[3]=SPI_ByteRead(RXB0D3);
	CAN_RX_Buf[4]=SPI_ByteRead(RXB0D4);
	CAN_RX_Buf[5]=SPI_ByteRead(RXB0D5);
	CAN_RX_Buf[6]=SPI_ByteRead(RXB0D6);
	CAN_RX_Buf[7]=SPI_ByteRead(RXB0D7);
	SPI_ByteWrite(CANINTF,0);
	//while(SPI_ByteRead(CANINTF)!=0);
}

void CAN_Eint_Clear(void)
{
	SPI_ByteWrite(CANINTF,0);
}



